#include <iostream>
#include <fstream>
#include <string>
#include <vector>
#include <sstream>

using namespace std;

int main() {

	// ######## CONSTANTS
	const int TYPE_IDENTIFIER = 8;
	// GRID
	const int GRID_ID_LENGTH = 8;
	const int GRID_CS_LENGTH = 8;
	const int GRID_X_LENGTH = 8;
	const int GRID_Y_LENGTH = 8;
	const int GRID_Z_LENGTH = 8;
	// QUAD4
	const int QUAD4_EID_LENGTH = 8;
	const int QUAD4_PID_LENGTH = 8;
	const int QUAD4_GX_LENGTH = 8;

	// ######## Arrays/Vectors
	// Grids
	vector<int> grid_id;
	vector<float> grid_X;
	vector<float> grid_Y;
	vector<float> grid_Z;
	// Quad4
	vector<int> quad4_eid;
	vector<int> quad4_pid;
	vector<int> quad4_g1;
	vector<int> quad4_g2;
	vector<int> quad4_g3;
	vector<int> quad4_g4;

	// ###### Variables

	ifstream fem_file;
	fem_file.open("bar.fem");

	//Check for error
	if (fem_file.fail()) {
		cerr << "Error opening file";
		return 0;
	}

	string line;
	//read file
	while (!fem_file.eof()) {
		//fem_file >> line;
		getline(fem_file, line);

		/// Recognize String
		if (line.substr(0, 1) != "$") { // Is comment?
			if (line.substr(0, TYPE_IDENTIFIER) == "GRID    ") {
				// ######## GRID
				// ## Format: GRID, ID, CS, X, Y, Z
				// ID
				int temp_id;
				stringstream id_ss(line.substr(TYPE_IDENTIFIER, GRID_ID_LENGTH));
				id_ss >> temp_id;
				grid_id.push_back(temp_id);
				// X
				float temp_coordinate;
				stringstream x_ss(line.substr(TYPE_IDENTIFIER + GRID_ID_LENGTH + GRID_CS_LENGTH, GRID_X_LENGTH));
				x_ss >> temp_coordinate;
				grid_X.push_back(temp_coordinate);
				// Y
				stringstream y_ss(line.substr(TYPE_IDENTIFIER + GRID_ID_LENGTH + GRID_CS_LENGTH + GRID_X_LENGTH, GRID_Y_LENGTH));
				y_ss >> temp_coordinate;
				grid_Y.push_back(temp_coordinate);
				// Z
				stringstream z_ss(line.substr(TYPE_IDENTIFIER + GRID_ID_LENGTH + GRID_CS_LENGTH + GRID_X_LENGTH + GRID_Y_LENGTH, GRID_Z_LENGTH));
				z_ss >> temp_coordinate;
				grid_Z.push_back(temp_coordinate);

			} else if (line.substr(0, TYPE_IDENTIFIER) == "CQUAD4  ") {
				// ######## QUAD4
				// ## Format: QUAD4, EID, PID, G1, G2, G3, G4
				// EID
				int temp_id;
				stringstream eid_ss(line.substr(TYPE_IDENTIFIER, QUAD4_EID_LENGTH));
				eid_ss >> temp_id;
				quad4_eid.push_back(temp_id);
				// PID
				stringstream pid_ss(line.substr(TYPE_IDENTIFIER + QUAD4_EID_LENGTH, QUAD4_PID_LENGTH));
				pid_ss >> temp_id;
				quad4_pid.push_back(temp_id);
				// G1
				stringstream g1_ss(line.substr(TYPE_IDENTIFIER + QUAD4_EID_LENGTH + QUAD4_PID_LENGTH, QUAD4_GX_LENGTH));
				g1_ss >> temp_id;
				quad4_g1.push_back(temp_id);
				// G2
				stringstream g2_ss(line.substr(TYPE_IDENTIFIER + QUAD4_EID_LENGTH + QUAD4_PID_LENGTH + QUAD4_GX_LENGTH, QUAD4_GX_LENGTH));
				g2_ss >> temp_id;
				quad4_g2.push_back(temp_id);
				// G3
				stringstream g3_ss(line.substr(TYPE_IDENTIFIER + QUAD4_EID_LENGTH + QUAD4_PID_LENGTH + 2 * QUAD4_GX_LENGTH, QUAD4_GX_LENGTH));
				g3_ss >> temp_id;
				quad4_g3.push_back(temp_id);
				// G4
				stringstream g4_ss(line.substr(TYPE_IDENTIFIER + QUAD4_EID_LENGTH + QUAD4_PID_LENGTH + 3 * QUAD4_GX_LENGTH, QUAD4_GX_LENGTH));
				g4_ss >> temp_id;
				quad4_g4.push_back(temp_id);
			}

		}
	}

	//Close stream
	fem_file.close();

	// DEBUG
	for (unsigned int i = 0; i < grid_id.size(); i++) {
		cout << "ID " << grid_id[i] << " X " << grid_X[i] << " Y " << grid_Y[i] << " Z " << grid_Z[i] << endl;
	}

	for (unsigned int i = 0; i < quad4_eid.size(); i++) {
		cout << "EID " << quad4_eid[i]<< " PID " << quad4_pid[i] << " G1 " << quad4_g1[i] << " G2 " << quad4_g2[i]<< " G3 " << quad4_g3[i]<< " G4 " << quad4_g4[i]<< endl;
	}


	return 0;
}
